package org.ellisande.grid.tags;

import java.util.EnumSet;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;


@Tag
@Info("Wrapping tag for a set of inputs. Allows you to group inputs under a single control.")
public class FieldSet extends AbstractTag {

	private static final long serialVersionUID = -4897725628291374278L;
	
	private static final String CONTROL_STYLE = "control-group";
	
	private static enum State{
		NORMAL(""),WARNING("warning"),ERROR("error"),SUCCESS("success");
		
		private String type;
		
		private State(String type){
			this.type = type;
		}
		
		public String style(){
			return type;
		}
	}
	
	@Attribute
	@Description("Defines the state of this form element. Allowed values: normal | warning | error | success. Default value: normal.")
	private State state = State.NORMAL;

	@Override
	public void startTag() throws Exception {
		TagBuilder fieldset = createTagBuilder("fielset");
		fieldset.addStyle(CONTROL_STYLE);
		fieldset.addStyle(state.style());
		
		JspWriter out = pageContext.getOut();
		out.write(fieldset.compileStartTag());
	}

	@Override
	public void endTag() throws Exception {
		JspWriter out = pageContext.getOut();
		out.write("</fieldset>");

	}
	
	@Override
	public void release(){
		super.release();
		state = null;
	}
	
	public void setState(String newState){
		for(State state : EnumSet.allOf(State.class)){
			if(state.name().equalsIgnoreCase(newState)){
				this.state = state;
				return;
			}
		}
	}

}
